//
// Created by dave on 18-7-29.
//

#pragma once
#include <string>
#include <vector>
#include <atomic>
#include "channel_handler.h"
#include "../db/location_dao.h"
#include "../concurrent/pooled_data_saver.h"

class jtt808_handler : public channel_handler {

public:
    jtt808_handler(pooled_data_saver<location>& saver, std::atomic_int& counter) : saver_(saver), counter_(counter) { }

    void on_open(std::shared_ptr<channel> ch) { }

    void on_read(std::shared_ptr<channel> ch, const unsigned char* buf, int size);

    void on_error(std::shared_ptr<channel> ch, boost::system::error_code ec);

    void on_close(std::shared_ptr<channel> ch, boost::system::error_code ec);

private:
    void process_frame(std::shared_ptr<channel> ch, const unsigned char* ptr, int start, int end);

    int find_mark(const unsigned char* ptr, int start, int stop);

private:
    std::string sim_;
    std::atomic_int& counter_;
    pooled_data_saver<location>& saver_;
};

